mui.init()
var bookshelf = new Vue({
	el: '.main',
	data: {
		cls: [],
		booksl: [],
		pingfen: [],
		trues: [],
		texts: []
	},
	updated: function() {
		$(".mainclass").on("tap", function(e) {
			if(!$(e.target).is('button')){
				common.open("../bookdetails/details.html", "", $(this).attr("bookuid"))
			}
			
		})
	}
});


function addFly(event,a) {	
		    var offset = $('#amount').offset(), flyer = $('<img class="u-flyer" id="imgd" src="../../imges/1368_smallPic_60.png" />');
		    flyer.fly({
		        start: {
		            left: event.pageX-40,
		            top: event.pageY-10
		        },
		        end: {
		            left: offset.left,
		            top: offset.top,
		            width: 24,
		            height: 24,
		            
		        }
		    });
		}
		$(function(){
			$(this).on("click",".added",function(){
			
//           var obj = {}
//			var self = $(this)
//			var data = $(this).parents(".mainclass")
//			obj.bookid = data.attr("bookid")
//			obj.booksrc = data.find("img").attr("src")
//			obj.bookname = data.find(".title_name").text()
			
//				alert('ddd')
				addFly(event);
			})
		})









var mask = mui.createMask(reduction)
$(".select").on("tap", ".mui-btn", function() {
	mask.show()
	var index = $(this).index()
	reduction()
	if($(".genre").eq(index).height() != 0) {
		mask.close()
		return
	}
	var height = $(".genre").eq(index).find("div").eq(0).height() * $(".genre").eq(index).find("div").length*2
	$(".genre").eq(index).css("height", height)
	$(".down").eq(index).css("transform", "rotate(180deg)")
	$(this).find("div").css({
		"color": "#21B0FD",
		"border-top-color": "#21B0FD"
	})
})

function reduction() {
	$(".genre").css("height", 0)
	$(".down").css("transform", "rotate(0deg)")
	$(".select").find("div").css({
		"color": "#333",
		"border-top-color": "#333"
	})
}
$(".typemain .popular").on("tap", "div", function() {
	$(this).parents(".typemain").prev().find(".selectiontype").eq(1).attr("uid", $(this).attr('uid'))
})
common.ajax("book/category", {
	uid: common.uid,
	token: common.token
}, function(data) {
	if(data.code == 200) {
		bookshelf.cls = data.data
	} else {
		mui.toast(data.message)
	}
})

var temp = 1

$(".genre").on("tap", "div", function() {
	var parent = $(this).parent().index()
	reduction()
	mask.close()
	$(".selectiontype").eq(parent).text($(this).text())
	bookshelf.trues = []
	bookshelf.booksl = []
	bookshelf.pingfen = []
	var grad = $(".selectiontype").eq(0).text() == "适合年级" ? "" : $(".selectiontype").eq(0).text()
	var types = $(".selectiontype").eq(1).attr("uid") == null ? "" : $(".selectiontype").eq(1).attr("uid")
	common.ajax('student/bookCity/queryRecommendBook', {
		uid: common.uid,
		token: common.token,
		grade: grad,
		type: types,
		page: 1,
		pageSize: "8"

	}, function(data) {
		if(data.code == 200) {
			//激活下拉刷新
			temp = 1

			mui('#offCanvasContentScroll').pullRefresh().refresh(true);
			datafor(data)
			if(!data.data.hasNextPage) {
				$(".mui-pull-bottom-pocket").css("visibility", "inherit")
				$(".mui-pull").text("已经没有更多了")
				mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh(true)
			}

		} else {
			mui.toast(data.message)
		}
	}, "get")
})

mui.init({
	pullRefresh: {
		container: '#offCanvasContentScroll',
		down: {
			callback: pulldownRefresh
		},
		up: {
			contentrefresh: '正在加载...',
			callback: pullupRefresh
		}
	}
});
/**
 * 下拉刷新具体业务实现
 */
function pulldownRefresh() {
	setTimeout(function() {
		mui('#offCanvasContentScroll').pullRefresh().endPulldownToRefresh()
	}, 400)

}
/**
 * 上拉加载具体业务实现
 */
function pullupRefresh() {
	var self = this
	var grad = $(".selectiontype").eq(0).text() == "适合年级" ? "" : $(".selectiontype").eq(0).text()
	var types = $(".selectiontype").eq(1).attr("uid") == null ? "" : $(".selectiontype").eq(1).attr("uid")
	common.ajax('student/bookCity/queryRecommendBook', {
		uid: common.uid,
		token: common.token,
		grade: grad,
		type: types,
		page: temp++,
		pageSize: "8"
	}, function(data) {
		if(data.code == 200) {
				mui('#offCanvasContentScroll').pullRefresh().refresh(true);
			if(!data.data.hasNextPage) {
				mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh(true)
			} else {
				mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh()
			}
			datafor(data)
		} else {
			mui.toast(data.message)
		}
	}, "get")
}

$('.shuchen', window.parent.document).on("tap", function() {
	temp = 1
	bookshelf.trues = []
	bookshelf.booksl = []
	bookshelf.pingfen = []
	shauxin()
})
setTimeout(function() {
	shauxin()
}, 30)

function shauxin() {
	if(mui.os.plus) {
		mui.plusReady(function() {
			setTimeout(function() {
				mui('#offCanvasContentScroll').pullRefresh().pullupLoading();
			}, 1000);

		});
	} else {
		mui.ready(function() {
			mui('#offCanvasContentScroll').pullRefresh().pullupLoading();
		});
	}

}

function datafor(data) {
	for(var i = 0; i < data.data.list.length; i++) {
		if(String(data.data.list[i].mark).indexOf(".") != -1) {
			bookshelf.trues.push("scoring_two")
		} else {
			bookshelf.trues.push("")
		}
		bookshelf.booksl.push(data.data.list[i])
		bookshelf.pingfen.push(Math.floor(data.data.list[i].mark))
	}
	bookshelf.$nextTick(function() {
		jilu()
	})

}



$(document).on("tap",".toggle", function(e) {
		if($(this).hasClass("add")) {
			var addbook = JSON.parse(localStorage.getItem('books')) || []
			var temp = []
			var obj = {}
			var self = $(this)
			var data = $(this).parents(".mainclass")
			obj.bookid = data.attr("bookid")
			obj.booksrc = data.find("img").attr("src")
			obj.bookname = data.find(".title_name").text()
			for(var i = 0; i < addbook.length; i++) {
				temp.push(addbook[i].bookid)
			}
			if(temp.indexOf(obj.bookid) == -1) {
				
				addbook.push(obj)
				self.text("已添加")
				self.removeClass("add")
				self.addClass("added")
			} else {
				
				var self = $(this)
				$(".added").on("tap", function() {
					localStorage.removeItem('books')
				})
				//				mui.toast("已经添加计划!")
				//              localStorage.removeItem('books')
			}
			localStorage.setItem('books', JSON.stringify(addbook))
		} else {
			
			$(this).addClass("add")
			$(this).text("添加")
			$(this).removeClass("added")
			var addbook = []
			for(var i = 0; i < $(".added").length; i++) {
				var obj = {}
                var self = $(this)
				obj.bookid = $(".added").eq(i).parents(".mainclass").attr("bookid")
				obj.booksrc = $(".added").eq(i).parents(".mainclass").find(".format img").attr("src")
				obj.bookname = $(".added").eq(i).parents(".mainclass").find(".title_name").text()
				addbook.push(obj)
			}
			console.log(addbook)
			localStorage.setItem('books', JSON.stringify(addbook))
			
		}
		window.parent.adden()
		e.stopPropagation()
		return false
	})











function jilu() {
	add()
}


function add() {
	var addbook = JSON.parse(localStorage.getItem('books')) || []
	$(".toggle").removeClass("added")
	$(".toggle").addClass("add")
	$(".toggle").text("添加")
	$.each(addbook, function(e, a) {
		$.each($(".mainclass"), function(index, txt) {
			if($(this).attr("bookid") == a.bookid) {
				$(this).find(".add").addClass("added")

				$(this).find(".add").text("已添加")
				$(this).find(".add").removeClass("add")
			}

		});
	});
}